package org.visionlibrary.image.geomtric.lines.histogram;

import java.util.List;

import org.visionlibrary.image.geomtric.lines.histogram.model.AbstractLinesHistogram;
import org.visionlibrary.image.geomtric.model.Vector2d;
import org.visionlibrary.image.model.Histogram;


/**
 * Klasa realizujaca generowanie histogramu dla zbioru odcinkow na podstawie ich
 * dlugosci.
 */
public class LineLength extends AbstractLinesHistogram {
	protected int maxLength = 100;
	protected int histogramSize = 1000;

	public LineLength(int maxLength, int histogramSize) {
		super();
		this.maxLength = maxLength;
		this.histogramSize = histogramSize;
	}

	@Override
	public Histogram process(List<Vector2d> lines) {
		if (null == lines)
			throw new NullPointerException("Lines list is null.");

		Histogram histogram = new Histogram(histogramSize + 1);
		for (Vector2d line : lines) {
			double length = line.length();
			int index = (int) t(length  , histogramSize);
			histogram.addToBin(index, 1);
		}

		return histogram;
	}

	private double t(double length, int precision) {
		double thetadiff = maxLength - length;
		return precision - (precision * thetadiff / maxLength);
	}
}
